<?php
set_time_limit(0); 
ini_set('memory_limit','256M');
require_once("./config.php");
header("Content-Type: text/html;charset=utf-8");


function my_dir($dir) {
    $files = array();
    if(@$handle = opendir($dir)) { //注意这里要加一个@，不然会有warning错误提示：）
        while(($file = readdir($handle)) !== false) {
            if($file != ".." && $file != ".") { //排除根目录；
                if(is_dir($dir."/".$file)) { //如果是子文件夹，就进行递归
                    $files[$file] = my_dir($dir."/".$file);
                } else { //不然就将文件的名字存入数组；
                    $files[] = $file;
                }
 
            }
        }
        closedir($handle);
        return $files;
    }
}

function listfile($ssdm)
{
	$content = json_decode($ssdm,true);
	$outdir = "D:/workdata/test/chsi/rs/";
	foreach($content as $key=>$val){
		$curr = $outdir."/".$val['dm']."/";
		//bianliwenjian
		$files =my_dir($curr);
		$str = "";
		foreach($files as $fkey=>$fval){
				$handle  = fopen ($curr.$fval, "r");

				$where = explode("_",$fval);

				$tt = array();
				$tt['ssdm'] = $where[0];
				$tt['dwmc'] = '';
				$tt['mldm'] = $where[1];
				$tt['mlmc'] = '';
				$tt['yjxkdm'] = $where[2];
				$tt['zymc'] = iconv("gbk","UTF-8",$where[3]);
				$tt['xxfs'] = substr($where[4], 0, 1 );;

				while (!feof ($handle)){
					$buffer  = fgets($handle);
					if(strlen($buffer) < 10){
						break;
					}
					$school = json_decode($buffer,true);


					$name = explode(")",$school[0]);
					$scname = $name[1];
					$sccode = trim(str_replace("(","",$name[0]));
					$sctag  = $school[2];
					$tagpg  = $school[3] =="&#xe664;" ? 1 :0;
					$tagself= $school[4] =="&#xe664;" ? 1 :0;
					$tagdc= $school[5] =="&#xe664;" ? 1 :0;



					$str.="insert chsi_school set ssdm='".$tt['ssdm']."'";
					$str.=", mldm='".$tt['mldm']."'";
					$str.=", yjxkdm='".$tt['yjxkdm']."'";
					$str.=", zymc='".$tt['zymc']."'";
					$str.=", xxfs='".$tt['xxfs']."'";
					$str.=", scname='".$scname."'";
					$str.=", sccode='".$sccode."'";
					$str.=", sctag='".$sctag."'";  //985 2111
					$str.=", tagpg='".$tagpg."'";  //研究生院
					$str.=", tagself='".$tagself."'";  //自划
					$str.=", tagdc='".$tagdc."';\n";  //博士点

					//echo $str;

					//exit;

				}// end while

		}// end file
		$filename = 'D:/workdata/test/chsi/sql/'.$val['dm'].".sql";
		$fh=fopen($filename, "w+");
		fwrite($fh, $str);    // 输出：6
		fclose($fh);
		echo $filename."<br>";

		
	}// end city

}

listfile($ssdm);

// 专业查询
$url="https://yz.chsi.com.cn/zsml/queryAction.do";
$data['ssdm'] = 12;
$data['dwmc'] = '';
$data['mldm'] = 'zyxw';
$data['mlmc'] = '';
$data['yjxkdm'] = '0252';
$data['zymc'] = '应用统计';
$data['xxfs'] = '';

/*理论查询

ssdm: 11
dwmc: 
mldm: 02
mlmc: 
yjxkdm: 0201
zymc: 理论经济学
xxfs: 
*/


//$html = test_post($url,$data);
//print_r(json_decode($ssdm,true));








?>